import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { DeleteResult, Repository, UpdateResult } from 'typeorm';
import { Bk_Curriculum_Structure } from './bk_curriculum_structure.entity';

@Injectable()
export class CurriculumStructureService {
    constructor(
        @InjectRepository(Bk_Curriculum_Structure)
        private readonly subjectRepository: Repository<Bk_Curriculum_Structure>
    ) { }

    async add(course: any): Promise<Bk_Curriculum_Structure> {
        return await this.subjectRepository.save(course);
    }

    async delete(id: number): Promise<DeleteResult> {
        return await this.subjectRepository.delete(id);
    }

    async update(id: number, course: any): Promise<UpdateResult> {
        return await this.subjectRepository.update(id, course);
    }

    async findAll(query: any): Promise<[Bk_Curriculum_Structure[], number]> {
        return await this.subjectRepository.findAndCount({
            relations: ["category"],
            order: { order_number: "ASC" },
            skip: query.skip,
            take: query.take
        });
    }

    async getCoursesByCategory(query: any): Promise<[Bk_Curriculum_Structure[], number]> {
        return await this.subjectRepository.findAndCount({
            relations: ["category"],
            order: { order_number: "ASC" },
            where: { category: { id: query.categoryId } },
            skip: query.skip,
            take: query.take
        });
    }
}
